Activity 4 Passing Variables on the URL

Problem 1

Create a simple form with two input fields for the user to enter two numbers performs arithmetic operations (addition, subtraction, Multiplication, division) on them. the form submits the data to a PHPscript using the GET methods.

Solution

if(isset($_GET["Number1"]) && isset($_GET["Options"]) && isset($_GET["Number2"])) {
    $num1 = $_GET["Number1"];
    $Options = $_GET["Options"];
    $num2 = $_GET["Number2"];

    if($Options == "addition") {
        $result = $num1 + $num2;
    } else if($Options == "subtraction") {
        $result = $num1 - $num2;
    } else if($Options == "multiplication") {
        $result = $num1 * $num2;
    } else if($Options == "division") {
        $result = $num1 / $num2;
    }

    echo "Result: " . $result;
}

Here's is the demo

Problem 2

Create a simple form that allows to input a message and counts the number of words in it. the forms the forms submits the data to a PHP script using the GET method.

Solution

if(isset($_GET["Message"])){
    $length = $_GET["Message"];
    echo "Words Count Result: " . (str_word_count($length));
}

Here's is the the demo